const BASE_URL = 'localhost';
const WS_PORT = '8088';
const WS_ADDRESS = `ws://${BASE_URL}:${WS_PORT}`;


export const useWebSocket = (handleMessage) => {
    const ws = new WebSocket(WS_ADDRESS);

    const init = () => {
        ws.addEventListener('open', handleOpen, false);
        ws.addEventListener('close', handleClose, false)
        ws.addEventListener('error', handleError, false)
        ws.addEventListener('message', handleMessage, false)
    }

    function handleOpen(e) {
        console.log('WebSocket open', e);
    }

    function handleClose(e) {
        console.log('WebSocket close', e);
    }

    function handleError(e) {
        console.log('WebSocket error', e);
    }

    init();
    return ws;
}